package com.weixingtu.commuterCar.servlet;

import com.weixingtu.commuterCar.bean.Device;
import com.weixingtu.commuterCar.bean.Message;
import com.weixingtu.commuterCar.service.DeviceService;
import com.weixingtu.commuterCar.service.impl.DeviceServiceImpl;
import com.weixingtu.commuterCar.util.JSONUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 查询设备表的所有数据
 */
@WebServlet(urlPatterns = "/allDevice.do")
public class AllDeviceServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DeviceService service = new DeviceServiceImpl();
        List<Device> list = new ArrayList<>();

        list = service.queryAll();

        Message msg = new Message();
        if(list == null || list.size() == 0){
            msg.setStatus(-1);
            msg.setResult("查询结果为空");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list);
        }

        String json = JSONUtil.toJSON(msg);
        PrintWriter writer = response.getWriter();
        writer.print(json);
    }
}
